/**
 * 
 */
package it.uniroma3.dia.digraph.algorithm.utility;

import java.util.InputMismatchException;

/**
 * @author BarycentricCoords
 *
 */
class Matrix {
	
	
	/**
	 * Calculates the determinant of a 2x2 matrix
	 * @param aij the values
	 * @return the determinant of a 2x2 matrix
	 */
	static double det(double a11, double a12, double a21, double a22){
		return a11*a22 - a12*a21;
	}
	
	/**
	 * Calculates the determinantof a 3x3 matrix
	 * @param aij the values
	 * @return the determinant of a 3x3 matrix
	 */
	static double det(
		double a11, double a12, double a13, 
		double a21, double a22, double a23, 
		double a31, double a32, double a33){
		return a11*a22*a33 + a12*a23*a31 + a13*a21*a32 -
			  (a31*a22*a13 + a32*a23*a11 + a33*a21*a12);
	}
	
	/**
	 * Calculates the determinant of a nxn matrix
	 * @param a the matrix
	 * @return the determinant of a nxn matrix
	 */
	 static double det(double[][] a){
		
		if (a.length==3 && a[0].length==3 && a[1].length==3 && a[2].length==3)
			return det(a[0][0], a[0][1], a[0][2], a[1][0], a[1][1], a[1][2], a[2][0], a[2][1], a[2][2]);
		if(a.length==2 && a[0].length==2 && a[1].length==2)
			return det(a[0][0], a[0][1], a[1][0], a[1][1]);
		
		//check input size
		boolean sizeCondition = true;		
		for (double[] row : a)
			sizeCondition &= (row.length == a.length);
		if (sizeCondition)
			throw new InputMismatchException("not yet implemented: currently just 2x2 o 3x3 matrices");
		
		// bad size: throw exception (it should be some exception type other than "input mismatch"...)
		throw new InputMismatchException("bad size input matrix: must be n x n");
		
	}

}
